<template>
	<view class="modify">
		<u-form :model="baseform" ref="baseform" v-if="showname=='用户姓名'||showname=='联系方式'">
			<view v-if="showname=='用户姓名'">
				<text>用户姓名：</text>
				<input placeholder="请填写" v-model="baseform.name" />
			</view>
			<!-- <view  v-if="showname=='身份证号'">
				<text>身份证号：</text>
				<input placeholder="请填写" v-model="baseform.card" />
			</view> -->
			<view  v-if="showname=='联系方式'">
				<text>联系方式：</text>
				<input placeholder="请填写" v-model="baseform.mobile" />
			</view>
		</u-form>
		<u-form :model="team_form" ref="team_form" v-else-if="showname=='班组名称'||showname=='班组负责人'||showname=='办公地址'">
			<view v-if="showname=='班组名称'">
				<text>班组名称：</text>
				<input placeholder="最多输入10个汉字" v-model="team_form.team"/>
			</view>
			<view v-else-if="showname=='班组负责人'">
				<text>班组负责人：</text>
				<input placeholder="请填写" v-model="team_form.team_leader_name" />
			</view>
			<view v-else>
				<text>办公地址：</text>
				<input placeholder="请填写" v-model="team_form.office_address" />
			</view>
		</u-form>
		<u-form :model="form" ref="form" v-else>
			<view v-if="showname=='工龄'">
				<text>工龄：</text>
				<input type="number" placeholder="请填写" v-model="form.work_years" />
			</view>
			<view  v-if="showname=='住址'">
				<text>住址：</text>
				<input placeholder="请填写" v-model="form.address" />
			</view>
		</u-form>
		<button class="submit" @click="submit">保存</button>
	</view>
</template>

<script>
	import api from '@/static/js/api.js'
	
	export default {
		data() {
			return {
				baseform:{
					name:'',
					card:'',
					mobile:''
				},
				showname: '',
				team_form: {
					team: '',
					team_leader_name: '',
					office_address: ''
				},
				name:'',
				form:{
					work_years:'',
					address:''
				},
				params:''
			}
		},
		onLoad(option) {
			this.showname = ''
			this.showname = option.p2
			if (option.p2 == '班组名称') {
				this.team_form.team = option.p1
			} else if (option.p2 == '班组负责人') {
				this.team_form.team_leader_name = option.p1
			} else if (option.p2 == '办公地址') {
				this.team_form.office_address = option.p1
			}else if (option.p2 == '工龄') {
				this.form.work_years = option.p1
			}else if (option.p2 == '住址') {
				this.form.address = option.p1
			}else if(option.p2 == '身份证号'){
				this.baseform.card = option.p1
			}else if(option.p2 == '用户姓名'){
				this.baseform.name = option.p1
			}else if(option.p2 == '联系方式'){
				this.baseform.mobile = option.p1
			}
		},
		methods: {
			submit() {
				if (this.showname == '班组名称') {
					this.edit('班组名称',this.$u.trim(this.team_form.team,'left'))
				} else if (this.showname == '班组负责人') {
					this.edit('班组负责人',this.$u.trim(this.team_form.team_leader_name,'left'))
				} else if(this.showname == '办公地址') {
					this.edit('办公地址',this.$u.trim(this.team_form.office_address,'left'))
				}else if(this.showname == '工龄') {
					this.edit('工龄',this.$u.trim(this.form.work_years,'left'))
				}else if(this.showname == '住址') {
					this.edit('住址',this.$u.trim(this.form.address,'left'))
				}else if(this.showname == '身份证号') {
					this.edit('身份证号',this.baseform.card)
				}else if(this.showname == '用户姓名') {
					if(this.$u.trim(this.baseform.name,'left')==''){
						uni.showToast({
							icon: 'none',
							title: '用户姓名不能为空'
						})
						return
					}
					this.edit('用户姓名',this.$u.trim(this.baseform.name,'left'))
				}else if(this.showname == '联系方式') {
					this.edit('联系方式',this.$u.trim(this.baseform.mobile,'left'))
				}
			},
			edit(p1,p2){
				var userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				if(userInfo.uid,userInfo.account_type=='工人'){
					if(p1=='用户姓名'){
						var name = p2!=''?p2:'-1'
						api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
							name:name
						}).then(res=>{
							uni.navigateBack()
						})
					}
				}else{
					if(p1=='用户姓名'){
						var username = p2!=''?p2:'-1'
						api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
							team_leader_name:username
						}).then(res=>{
							uni.navigateBack()
						})
					}
				}
				if(p1=='班组名称'){
					var team_name = p2!=''?p2:'-1'
					if(p2.replace(/[^\x00-\xff]/g, 'xx').length>20){
						uni.showToast({
							title: '班组名称最多10个汉字',
							icon: 'none'
						})
						return
					}
					api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
						team_name:team_name
					}).then(res=>{
						uni.navigateBack()
					})
				}else if(p1=='班组负责人'){
					var team_leader_name = p2!=''?p2:'-1'
					api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
						team_leader_name:team_leader_name
					}).then(res=>{
						uni.navigateBack()
					})
				}else if(p1=='办公地址'){
					var office_address = p2!=''?p2:'-1'
					api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
						office_address:office_address
					}).then(res=>{
						uni.navigateBack()
					})
				}else if(p1=='工龄'){
					var year = p2>0?p2:'-1'
					if(p2==0){
						uni.showToast({
							title: '请输入合理的数字',
							icon: 'none'
						})
						return
					}
					api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
						work_years:year
					}).then(res=>{
						uni.navigateBack()
					})
				}else if(p1=='住址'){
					var address = p2!=''?p2:'-1'
					api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
						address:address
					}).then(res=>{
						uni.navigateBack()
					})
				}
				// else if(p1=='身份证号'){
				// 	api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
				// 		// address:p2
				// 	}).then(res=>{
				// 		uni.navigateBack()
				// 	})
				// }
				else if(p1=='联系方式'){
					api.modifyInformation(userInfo.access_token,'res.partner','_api_editor_personal_information',userInfo.uid,userInfo.account_type,{
						// address:p2
					}).then(res=>{
						uni.navigateBack()
					})
				}
				
			}
		}
	}
</script>

<style lang="less" scoped>
	.modify {
		padding: 20rpx;

		/deep/.u-form>view {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 80rpx;
			margin: 0 auto;

			input {
				flex: 1;
				height: 44rpx;
				background: #EFF0F1;
				border-radius: 8rpx;
				padding: 14rpx 20rpx;
			}
		}

		/deep/.u-form-item {
			padding: 7rpx 0 !important;
		}

		/deep/.u-form-item::after {
			border: none;
		}

		/deep/.u-icon__icon::before {
			content: "\e605" !important;
		}

		/deep/.u-input__right-icon__item {
			margin-left: 0px !important;
		}

		/deep/.u-icon text {
			font-size: 14px !important;
			color: #c0c4cc !important;
		}

		.submit {
			background: #D73C4B;
			color: #fff;
			width: 40%;
			margin-top: 20rpx;
			height: 60rpx;
			font-size: 28rpx;
			line-height: 60rpx;
		}
	}
</style>
